package com.chance.cc.crawler.prod.command.job.domain.vm.xcar.realtime;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.domain.vm.xcar.XCarAutoCommonCrawlerSchedulerJob;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;

/**
 * @ClassName song
 * @Description TODO
 * @Author ding
 * 爱卡经销商采集
 * @Date 2021/8/29 10:06
 * @Version 1.0
 **/
public class XcarSeriesDiscountsRealtimeCrawlerScheduleJob extends XCarAutoCommonCrawlerSchedulerJob {
    public static String site = "discount";

    public static void main(String[] args) {
        publishXcarNewsSchedulerJob();
    }

    public static CrawlerJob publishXcarNewsSchedulerJob(){
        CrawlerJob crawlerJob = xcarCwalerJob();


        //发布定时采集作业
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(crawlerJob));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("爱卡汽车 车系经销商信息采集 每天 2点 定时采集");//TODO 定时采集时间未定
        crawlerScheduleJob.setCrawlerKey(crawlerJob.generateCrawlerKey());
        HttpPage page = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布作业：" + page.getRawText());

        return crawlerJob;
    }

    public static CrawlerJob xcarCwalerJob() {
        String url = "https://dealer.xcar.com.cn/";
        String site_biz = "realtime";//实时
        //String site_biz = "simple";
        HttpConfig httpConfig = HttpConfig.me(domain);
        httpConfig.setHttpSite(site);

        CrawlerRequestRecord record = CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPage)
                .httpConfig(httpConfig)
                .httpUrl(url)
                .proxy(proxy)
                .domain(domain)
                .releaseTime(System.currentTimeMillis())
                .httpHead("cookie","__jsluid_h=8b69f245411998b6f0ea95ba7c79c40e; __jsl_clearance=1570598566.464|0|h%2FbOP%2BtfLH%2BdlQOX27DEgliP0II%3D; _Xdwuv=5d9d67a86da0f; _Xdwnewuv=1; uv_firstv_refers=http%3A//dealer.xcar.com.cn/d1000/66.htm%3Ftype%3D1%26page%3D1; _PVXuv=5d9d64a4700af; isRemoveCookie=1; Hm_lvt_53eb54d089f7b5dd4ae2927686b183e0=1570598565; _locationInfo_=%7Burl%3A%22http%3A%2F%2Fsh.xcar.com.cn%2F%22%2Ccity_id%3A%22507%22%2Cprovince_id%3A%222%22%2C%20city_name%3A%22%25E4%25B8%258A%25E6%25B5%25B7%22%7D; fw_slc=1%3A1570598564%3B1%3A1570598956%3B1%3A1570598959%3B1%3A1570599006%3B1%3A1570599007; fw_exc=1%3A1570598562%3B1%3A1570598958%3B1%3A1570598960%3B1%3A1570598961%3B1%3A1570599012; fw_pvc=1%3A1570598564%3B1%3A1570598959%3B1%3A1570598961%3B1%3A1570599006%3B1%3A1570599012; fw_clc=1%3A1570598958%3B1%3A1570598960%3B1%3A1570599029%3B1%3A1570599039%3B1%3A1570599042; _Xdwstime=1570599046; Hm_lpvt_53eb54d089f7b5dd4ae2927686b183e0=1570599043")
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)  //TODO  过滤条件时间未定
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24,null))
                .resultLabelTag(CrawlerEnum.CrawlerDataType.article)
                .build();
        record.tagsCreator().bizTags().addDomain(domain);
        record.tagsCreator().bizTags().addSite(site);
        record.tagsCreator().bizTags().addSiteBiz(site_biz);

        //车系关键次
        CrawlerRequestRecord initNewsCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("xcar_series",turnPageItem)
                .httpUrl("http://192.168.1.217:9599/v1/meta/xcar/keys?site=distributor")//http://192.168.1.217:9959/v1/meta/xcar/keys?site=carSeries
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        CrawlerJob crawlerJob = CrawlerJob.builder()
                .crawlerJobThreadNumber(10)
                .triggerInfo(
                        domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),//xcar-discount-realtime-realtime-realtime
                        StringUtils.joinWith("-", site, site_biz,"realtime", CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(StringUtils.joinWith("-",domain,site,site_biz,"queue")))
                .kafkaResultPipeline("kafka", kafkaTopciForTraceJob, null)
                .fileResultPipeline("kafka","/data/chance_crawler_runner/logs/node/discount.log",false)
                .requestRecord(record)
                .supportRecord(initNewsCrawlerRecord)
                .build();
        return crawlerJob;
    }


}
